﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using TheOutfield.UmbExt.UniversalMediaPicker;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Interfaces;
using TheOutfield.UmbExt.UniversalMediaPicker.Providers;

namespace Skybrud.UmbExt.UniversalMediaPicker.Providers.Issuu
{
    public class IssuuProvider : AbstractProvider
    {
        private IssuuConfig config;
        private IConfigControl configControl;
        private ICreateControl createControl;

        public IssuuProvider()
        { }

        public IssuuProvider(string config)
            : base(config)
        {
            this.config = config.DeserializeJsonTo<IssuuConfig>();
        }

        public override string Alias
        {
            get { return "Issuu"; }
        }

        public override IConfigControl ConfigControl
        {
            get
            {
                if (configControl == null)
                    configControl = new IssuuConfigControl(this, Config);

                return configControl;
            }
        }

        public override ICreateControl CreateControl
        {
            get { throw new NotImplementedException(); }
            //get
            //{
            //    if (createControl == null)
            //        createControl = new IssuuCreateControl(this, Config);

            //    return createControl;
            //}
        }

        public override IList<MediaItem> GetMediaItems(string parentId)
        {
            var mediaItems = new List<MediaItem>();

            if(parentId == "-1")
            {
                //list folders
                var fldrs = new IssuuService(config.IssuuApiKey, config.IssuuApiSecret).GetFolders();
                foreach (var issuuFolder in fldrs)
                {
                    var mi = issuuFolder.ToMediaItem();
                    mi.Id = "fldr:" + mi.Id;
                    mediaItems.Add(mi);
                }

                //all public
                var miAll = new MediaItem
                             {
                                 Id = "all:-1",
                                 Title = "Public (All)",
                                 Icon = UmbracoIcons.FolderOpen,
                                 HasChildren = true,
                                 Selectable = false
                             };
                mediaItems.Add(miAll);

            } else if(parentId.StartsWith("fldr:"))
            {
                //list Issuu documents
                var docs = new IssuuService(config.IssuuApiKey, config.IssuuApiSecret).GetDocs();
                foreach (var issuuDoc in docs)
                {
                    if(issuuDoc.Folders.Contains(parentId.Split(':')[1]))
                        mediaItems.Add(issuuDoc.ToMediaItem());
                }
            } else if(parentId.StartsWith("all:"))
            {
                //list all public Issuu documents
                var docs = new IssuuService(config.IssuuApiKey, config.IssuuApiSecret).GetDocs();
                foreach (var issuuDoc in docs)
                {
                    mediaItems.Add(issuuDoc.ToMediaItem());
                }
            }

            return mediaItems;
        }

        public override MediaItem GetMediaItemById(string id)
        {
            //save the document on save event in Umbraco
            return new IssuuService(config.IssuuApiKey, config.IssuuApiSecret).GetDoc(id).ToMediaItem();
        }

        public IssuuService.IssuuDoc GetIssuuDocument(string id)
        {
            return new IssuuService(config.IssuuApiKey, config.IssuuApiSecret).GetDoc(id);
        }

        public Collection<IssuuService.IssuuDoc> GetIssuuDocuments()
        {
            return new IssuuService(config.IssuuApiKey, config.IssuuApiSecret).GetDocs();
        }
    }
}